// pages/CNKIProject/DailyReport/AddForm.js\
var app = getApp();
var util = require('../../../utils/util.js');
var requestRoot = app.globalData.hostUrl;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    projectlist: [],
    ProjectIndex: [0],
    projectSelect: '',
    worktypeSelect: '',

    formData_Last: {},
    formList_Last: [],
    formList_next: [],
    ViewModuleIndex: 0//当前添加到第几个项目了
  },
  /**
   * 赋值下周项目Id
   */
  bindPickerChange: function (e) {
    let target = e.currentTarget.dataset;
    let index = e.detail.value;
    this.setData({
      [`ProjectIndex[${target.field}]`]: index,
      [`formList_next[${target.field}].projectId`]: this.data.projectlist[index].value
    })

  },
  /**
   * 赋值工作内容
   */
  Last_jobContentChange: function (e) {
    let target = e.currentTarget.dataset;
    this.setData({
      [`formList_Last[${target.field}].jobContent`]: e.detail.value
    })
  },
  /**
   * 绑定开始时间
   * @param {} e 
   */
  bindStartDateChange: function (e) {
    let target = e.currentTarget.dataset;
    this.setData({
      [`formData_Last.startDate`]: e.detail.value
    })
  },
  bindEndDateChange: function (e) {
    let target = e.currentTarget.dataset;
    this.setData({
      [`formData_Last.endDate`]: e.detail.value
    })
  },
  /**
 * 赋值工作内容
 */
  Next_jobContentChange: function (e) {
    let target = e.currentTarget.dataset;
    this.setData({
      [`formList_next[${target.field}].jobContentsNext`]: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    wx.cloud.callFunction({
      name: 'get-projects',
      data: {
        url: requestRoot + '/api/Values/ProjectInfos',
        WEBJSESSIONID: wx.getStorageSync('WEBJSESSIONID'),
        token: wx.getStorageSync('csrf-token')
      },
      success: function (res) {
        _this.setData({
          projectlist: JSON.parse(res.result)
        })
      },
      complete: () => {
        //上周和下周计划
        wx.cloud.callFunction({
          name: 'Week-LastContent',
          data: {
            url: requestRoot + '/api/Week/LastWeekContent',
            WEBJSESSIONID: wx.getStorageSync('WEBJSESSIONID')
          },
          success: function (res) {
            var result = JSON.parse(res.result);
            if (result.code == 1) {
              _this.setData({
                formList_Last: result.data_last,
                formList_next: result.data_next == null ? {} : result.data_next,
                ViewModuleIndex: result.data_next == null ? 0 : result.data_next.length - 1,
                [`formData_Last.startDate`]: result.startDate,
                [`formData_Last.endDate`]: result.endDate
              })
              result.data_next.forEach(function (value, index) {//找到下周计划的角标
                var nextIndex = _this.data.projectlist.findIndex(function (Listvalue, index) {
                  return Listvalue.value == value.projectIdsNext;
                })
                debugger
                _this.setData({
                  [`ProjectIndex[${index}]`]: nextIndex
                })
              })
              // //放到数组里
              // _this.data.formList_next.forEach(function (Pvalue, index) {
              //   let curIndex = _this.data.projectlist.findIndex(function (Currentvalue) {
              //     Currentvalue.value == Pvalue.projectIdsNext
              //   })
              //   _this.data.formList_next[index].index = index;
              // })

            } else {
              wx.showToast({
                title: '上周工作内容加载失败'
              })
            }
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // if(this.data.formList_next.length==0)
    // {
    //   this.setData({
    //     [`formList_next[0].projectIdsNext`]: this.data.projectlist[0].value,
    //     [`formList_next[0].jobContentsNext`]:''
    //   })
    // }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
  * 添加项目
  */
  AddView: function () {
    this.data.ViewModuleIndex++;
    this.setData({
      [`formList_next[${this.data.ViewModuleIndex}].projectIdsNext`]: this.data.projectlist[0].value,
      [`formList_next[${this.data.ViewModuleIndex}].jobContentsNext`]: ''
    })
  },
  /**
   * 减少项目
   */
  ReduceView: function () {
    if (this.data.ViewModuleIndex > 0) {
      this.data.formList_next.pop();//返回被删除的数组元素
      var temp = this.data.formList_next;
      this.setData({
        formList_next: temp
      })
    }
  },
  submitForm: function () {
    var submitData = this.data.formData;
    let worktotal = 0;
    this.data.formList.forEach(function (item, index) {
      worktotal += parseInt(item.workload);
    })
    if (worktotal > 24) {
      wx.showToast({
        title: "一天24小时",
      })
    }
    submitData.workload = worktotal;

    submitData.list = JSON.stringify(this.data.formList_next);
    wx.cloud.callFunction({
      name: 'Daily-Submit',
      data: {
        submitData: submitData,
        url: requestRoot + "/API/Daily/add",
        token: wx.getStorageSync('csrf-token'),
        WEBJSESSIONID: wx.getStorageSync('WEBJSESSIONID')
      },
      success: function (e) {
        var res = JSON.parse(e);
        if (res.flag == true) {
          wx.showToast({
            title: e.msg,
          })
        } else {
          wx.showToast({
            title: e.msg,
          })
        }
      }
    })
  }
})